package com.winshare.saleAssistant.util;

import java.util.ArrayList;
import java.util.List;

public class LogicStatus {

    private static final int SC_OK = 200;
    private static final int SC_UNAUTHORIZED = 401;
    private static final int SC_NOT_FOUND = 404;
    private static final int SC_FAILURE = 409;
    private static final int SC_PARAM_ERROR = 430;
    private static final int SC_INTERNAL_SERVER_ERROR = 500;
    public static final LogicStatus SUCCESS = new LogicStatus(200, "成功");
    public static final LogicStatus UNAUTHORIZED = new LogicStatus(401, "未授权");
    public static final LogicStatus NOT_FOUND = new LogicStatus(404, "未找到");
    public static final LogicStatus FAILURE = new LogicStatus(409, "失败");
    public static final LogicStatus PARAM_ERROR = new LogicStatus(430, "参数错误");
    public static final LogicStatus SERVER_ERROR = new LogicStatus(500, "服务器错误");
    private static final List<LogicStatus> STATUS_BUILT_IN = new ArrayList();
    private static final String DEFAULT_REASON_PHRASE = "请详细定义该错误原因";
    private int value;
    private String reasonPhrase;

    public static LogicStatus of(int value, String reasonPhrase) {
        return new LogicStatus(value, reasonPhrase);
    }

    public static LogicStatus peekStatusBuiltInOrDefault(int value) {
        return (LogicStatus)STATUS_BUILT_IN.stream().filter((logicStatus) -> {
            return logicStatus.value == value;
        }).findFirst().orElseGet(() -> {
            return of(value, "请详细定义该错误原因");
        });
    }

    private LogicStatus(int value, String reasonPhrase) {
        this.value = value;
        this.reasonPhrase = reasonPhrase;
    }

    private LogicStatus() {
    }

    public int getValue() {
        return this.value;
    }

    public String getReasonPhrase() {
        return this.reasonPhrase;
    }

    public LogicStatus mutate(String reasonPhrase) {
        return new LogicStatus(this.value, reasonPhrase);
    }

    static {
        STATUS_BUILT_IN.add(SUCCESS);
        STATUS_BUILT_IN.add(FAILURE);
        STATUS_BUILT_IN.add(UNAUTHORIZED);
        STATUS_BUILT_IN.add(NOT_FOUND);
        STATUS_BUILT_IN.add(PARAM_ERROR);
        STATUS_BUILT_IN.add(SERVER_ERROR);
    }
}
